﻿// =================================================================== 
// 项目说明
//====================================================================
// Wup。@Copy Right 2014
// 文件： Course.cs
// 项目名称：买车网
// 创建时间：2014/7/20
// 负责人：Wup
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;


namespace Weixin.DAL.ChinaTax
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Course.
    /// </summary>
    public partial class CourseDataAccessLayer 
    {
		#region 构造函数


		/// <summary>
		/// 
		/// </summary>
		public CourseDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_CourseModel">Course实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(CourseEntity _CourseModel)
		{
			string sqlStr="insert into Course([Title],[StartDate],[EndDate],[Province],[City],[Teacher],[CTypeId],[Detail],[CreateTime],[UpdateTime],[UserName],[IsActive],[Address]) values(@Title,@StartDate,@EndDate,@Province,@City,@Teacher,@CTypeId,@Detail,@CreateTime,@UpdateTime,@UserName,@IsActive,@Address) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@Title",SqlDbType.VarChar),
			new SqlParameter("@StartDate",SqlDbType.Date),
			new SqlParameter("@EndDate",SqlDbType.Date),
			new SqlParameter("@Province",SqlDbType.VarChar),
			new SqlParameter("@City",SqlDbType.VarChar),
			new SqlParameter("@Teacher",SqlDbType.VarChar),
			new SqlParameter("@CTypeId",SqlDbType.Int),
			new SqlParameter("@Detail",SqlDbType.VarChar),
			new SqlParameter("@CreateTime",SqlDbType.DateTime),
			new SqlParameter("@UpdateTime",SqlDbType.DateTime),
			new SqlParameter("@UserName",SqlDbType.VarChar),
			new SqlParameter("@IsActive",SqlDbType.Int),
            new SqlParameter("@Address",SqlDbType.VarChar)
			};			
			_param[0].Value=_CourseModel.Title;
			_param[1].Value=_CourseModel.StartDate;
			_param[2].Value=_CourseModel.EndDate;
			_param[3].Value=_CourseModel.Province;
			_param[4].Value=_CourseModel.City;
			_param[5].Value=_CourseModel.Teacher;
			_param[6].Value=_CourseModel.CTypeId;
			_param[7].Value=_CourseModel.Detail;
			_param[8].Value=_CourseModel.CreateTime;
			_param[9].Value=_CourseModel.UpdateTime;
			_param[10].Value=_CourseModel.UserName;
			_param[11].Value=_CourseModel.IsActive;
            _param[12].Value=_CourseModel.Address;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_CourseModel">Course实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(SqlTransaction sp,CourseEntity _CourseModel)
		{
            string sqlStr = "insert into Course([Title],[StartDate],[EndDate],[Province],[City],[Teacher],[CTypeId],[Detail],[CreateTime],[UpdateTime],[UserName],[IsActive],[Address]) values(@Title,@StartDate,@EndDate,@Province,@City,@Teacher,@CTypeId,@Detail,@CreateTime,@UpdateTime,@UserName,@IsActive,@Address) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@Title",SqlDbType.VarChar),
			new SqlParameter("@StartDate",SqlDbType.Date),
			new SqlParameter("@EndDate",SqlDbType.Date),
			new SqlParameter("@Province",SqlDbType.VarChar),
			new SqlParameter("@City",SqlDbType.VarChar),
			new SqlParameter("@Teacher",SqlDbType.VarChar),
			new SqlParameter("@CTypeId",SqlDbType.Int),
			new SqlParameter("@Detail",SqlDbType.VarChar),
			new SqlParameter("@CreateTime",SqlDbType.DateTime),
			new SqlParameter("@UpdateTime",SqlDbType.DateTime),
			new SqlParameter("@UserName",SqlDbType.VarChar),
			new SqlParameter("@IsActive",SqlDbType.Int),
            new SqlParameter("@Address",SqlDbType.VarChar)
			};			
			_param[0].Value=_CourseModel.Title;
			_param[1].Value=_CourseModel.StartDate;
			_param[2].Value=_CourseModel.EndDate;
			_param[3].Value=_CourseModel.Province;
			_param[4].Value=_CourseModel.City;
			_param[5].Value=_CourseModel.Teacher;
			_param[6].Value=_CourseModel.CTypeId;
			_param[7].Value=_CourseModel.Detail;
			_param[8].Value=_CourseModel.CreateTime;
			_param[9].Value=_CourseModel.UpdateTime;
			_param[10].Value=_CourseModel.UserName;
			_param[11].Value=_CourseModel.IsActive;
            _param[12].Value = _CourseModel.Address;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(sp,CommandType.Text,sqlStr,_param));
			return res;
		}


		/// <summary>
		/// 向数据表Course更新一条记录。
		/// </summary>
		/// <param name="_CourseModel">_CourseModel</param>
		/// <returns>影响的行数</returns>
		public int Update(CourseEntity _CourseModel)
		{
            string sqlStr = "update Course set [Title]=@Title,[StartDate]=@StartDate,[EndDate]=@EndDate,[Province]=@Province,[City]=@City,[Teacher]=@Teacher,[CTypeId]=@CTypeId,[Detail]=@Detail,[CreateTime]=@CreateTime,[UpdateTime]=@UpdateTime,[UserName]=@UserName,[IsActive]=@IsActive,[Address]=@Address where Cid=@Cid";
			SqlParameter[] _param={				
				new SqlParameter("@Cid",SqlDbType.Int),
				new SqlParameter("@Title",SqlDbType.VarChar),
				new SqlParameter("@StartDate",SqlDbType.Date),
				new SqlParameter("@EndDate",SqlDbType.Date),
				new SqlParameter("@Province",SqlDbType.VarChar),
				new SqlParameter("@City",SqlDbType.VarChar),
				new SqlParameter("@Teacher",SqlDbType.VarChar),
				new SqlParameter("@CTypeId",SqlDbType.Int),
				new SqlParameter("@Detail",SqlDbType.VarChar),
				new SqlParameter("@CreateTime",SqlDbType.DateTime),
				new SqlParameter("@UpdateTime",SqlDbType.DateTime),
				new SqlParameter("@UserName",SqlDbType.VarChar),
				new SqlParameter("@IsActive",SqlDbType.Int),
                new SqlParameter("@Address",SqlDbType.VarChar)
				};				
				_param[0].Value=_CourseModel.Cid;
				_param[1].Value=_CourseModel.Title;
				_param[2].Value=_CourseModel.StartDate;
				_param[3].Value=_CourseModel.EndDate;
				_param[4].Value=_CourseModel.Province;
				_param[5].Value=_CourseModel.City;
				_param[6].Value=_CourseModel.Teacher;
				_param[7].Value=_CourseModel.CTypeId;
				_param[8].Value=_CourseModel.Detail;
				_param[9].Value=_CourseModel.CreateTime;
				_param[10].Value=_CourseModel.UpdateTime;
				_param[11].Value=_CourseModel.UserName;
				_param[12].Value=_CourseModel.IsActive;
                _param[13].Value = _CourseModel.Address;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Course更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_CourseModel">_CourseModel</param>
		/// <returns>影响的行数</returns>
		public int Update(SqlTransaction sp,CourseEntity _CourseModel)
		{
            string sqlStr = "update Course set [Title]=@Title,[StartDate]=@StartDate,[EndDate]=@EndDate,[Province]=@Province,[City]=@City,[Teacher]=@Teacher,[CTypeId]=@CTypeId,[Detail]=@Detail,[CreateTime]=@CreateTime,[UpdateTime]=@UpdateTime,[UserName]=@UserName,[IsActive]=@IsActive,[Address]=@Address where Cid=@Cid";
			SqlParameter[] _param={				
				new SqlParameter("@Cid",SqlDbType.Int),
				new SqlParameter("@Title",SqlDbType.VarChar),
				new SqlParameter("@StartDate",SqlDbType.Date),
				new SqlParameter("@EndDate",SqlDbType.Date),
				new SqlParameter("@Province",SqlDbType.VarChar),
				new SqlParameter("@City",SqlDbType.VarChar),
				new SqlParameter("@Teacher",SqlDbType.VarChar),
				new SqlParameter("@CTypeId",SqlDbType.Int),
				new SqlParameter("@Detail",SqlDbType.VarChar),
				new SqlParameter("@CreateTime",SqlDbType.DateTime),
				new SqlParameter("@UpdateTime",SqlDbType.DateTime),
				new SqlParameter("@UserName",SqlDbType.VarChar),
				new SqlParameter("@IsActive",SqlDbType.Int),
                new SqlParameter("@Address",SqlDbType.VarChar)
				};				
				_param[0].Value=_CourseModel.Cid;
				_param[1].Value=_CourseModel.Title;
				_param[2].Value=_CourseModel.StartDate;
				_param[3].Value=_CourseModel.EndDate;
				_param[4].Value=_CourseModel.Province;
				_param[5].Value=_CourseModel.City;
				_param[6].Value=_CourseModel.Teacher;
				_param[7].Value=_CourseModel.CTypeId;
				_param[8].Value=_CourseModel.Detail;
				_param[9].Value=_CourseModel.CreateTime;
				_param[10].Value=_CourseModel.UpdateTime;
				_param[11].Value=_CourseModel.UserName;
				_param[12].Value=_CourseModel.IsActive;
                _param[13].Value = _CourseModel.Address;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}		
		
		
		/// <summary>
		/// 删除数据表Course中的一条记录
		/// </summary>
	    /// <param name="Cid">cid</param>
		/// <returns>影响的行数</returns>
		public int Delete(int Cid)
		{
			string sqlStr="delete from Course where [Cid]=@Cid";
			SqlParameter[] _param={			
			new SqlParameter("@Cid",SqlDbType.Int),
			
			};			
			_param[0].Value=Cid;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Course中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="Cid">cid</param>
		/// <returns>影响的行数</returns>
		public int Delete(SqlTransaction sp,int Cid)
		{
			string sqlStr="delete from Course where [Cid]=@Cid";
			SqlParameter[] _param={			
			new SqlParameter("@Cid",SqlDbType.Int),
			
			};			
			_param[0].Value=Cid;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  course 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>course 数据实体</returns>
		public CourseEntity Populate_CourseEntity_FromDr(DataRow row)
		{
			CourseEntity Obj = new CourseEntity();
			if(row!=null)
			{
				Obj.Cid = (( row["Cid"])==DBNull.Value)?0:Convert.ToInt32( row["Cid"]);
				Obj.Title =  row["Title"].ToString();
				Obj.StartDate = (( row["StartDate"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["StartDate"]);
				Obj.EndDate = (( row["EndDate"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["EndDate"]);
				Obj.Province =  row["Province"].ToString();
				Obj.City =  row["City"].ToString();
				Obj.Teacher =  row["Teacher"].ToString();
				Obj.CTypeId = (( row["CTypeId"])==DBNull.Value)?0:Convert.ToInt32( row["CTypeId"]);
				Obj.Detail =  row["Detail"].ToString();
				Obj.CreateTime = (( row["CreateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["CreateTime"]);
				Obj.UpdateTime = (( row["UpdateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["UpdateTime"]);
				Obj.UserName =  row["UserName"].ToString();
				Obj.IsActive = (( row["IsActive"])==DBNull.Value)?0:Convert.ToInt32( row["IsActive"]);
                Obj.Address = row["Address"].ToString();
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  course 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>course 数据实体</returns>
		public CourseEntity Populate_CourseEntity_FromDr(IDataReader dr)
		{
			CourseEntity Obj = new CourseEntity();
			
				Obj.Cid = (( dr["Cid"])==DBNull.Value)?0:Convert.ToInt32( dr["Cid"]);
				Obj.Title =  dr["Title"].ToString();
				Obj.StartDate = (( dr["StartDate"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["StartDate"]);
				Obj.EndDate = (( dr["EndDate"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["EndDate"]);
				Obj.Province =  dr["Province"].ToString();
				Obj.City =  dr["City"].ToString();
				Obj.Teacher =  dr["Teacher"].ToString();
				Obj.CTypeId = (( dr["CTypeId"])==DBNull.Value)?0:Convert.ToInt32( dr["CTypeId"]);
				Obj.Detail =  dr["Detail"].ToString();
				Obj.CreateTime = (( dr["CreateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["CreateTime"]);
				Obj.UpdateTime = (( dr["UpdateTime"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["UpdateTime"]);
				Obj.UserName =  dr["UserName"].ToString();
				Obj.IsActive = (( dr["IsActive"])==DBNull.Value)?0:Convert.ToInt32( dr["IsActive"]);
                Obj.Address = dr["Address"].ToString();
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Course对象
		/// </summary>
		/// <param name="cid">cid</param>
		/// <returns>Course对象</returns>
		public CourseEntity Get_CourseEntity (int cid)
		{
			CourseEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@Cid",SqlDbType.Int)
			};
			_param[0].Value=cid;
			string sqlStr="select * from Course with(nolock) where Cid=@Cid";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_CourseEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Course所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< CourseEntity> Get_CourseAll()
		{
			IList< CourseEntity> Obj=new List< CourseEntity>();
			string sqlStr="select * from Course";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_CourseEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="cid">cid</param>
        /// <returns>是/否</returns>
		public bool IsExistCourse(int cid)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@cid",SqlDbType.Int)
                                  };
            _param[0].Value=cid;
            string sqlStr="select Count(*) from Course where Cid=@cid";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
